<include file="../Public/header" />
<link href="{:APPS_URL}/w3g/_static/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
  body{background-color: #f8f8f8;}
</style>
<div id="content">
    <div id="weiba" data-title="绑定手机" class="panel" data-selected="true"  data-menu="msgmenu" style="padding:0;">
        <header>
            <div id="header-buttons">
                <a href="javascript:history.go(-1);">
                    <div id="back" class="header-menu-link" data-back="false">
                    </div>
                </a>
            </div>
            <h1 class="hasback">
                绑定手机
            </h1>
        </header>
    <div class="set-item" id="">
      <form id="do_binding_mobile" method="post">
         <div class="form-set">
            <dl>
                <dd><input type="text" autocomplete="off" class="text1" name="mobile" id="write_mobile" placeholder="请输入手机号"></dd>
            </dl>
            <dl class="noborder">
                <dd>
                  <input type="text" autocomplete="off" class="text1" name="mobile_code" id="write_mobile_code" placeholder="验证码" style="width:34%;">
                  <a class="btn_gray" onclick="send_moblie_msg();return false;" mobile="0" id="get_mobile_code" href="javascript:;"><span>获取验证码</span></a>
                  <div style="display:none;" id="mobile_msg_tip" class="txt-info">验证码已发送到您的手机，30分钟内输入有效，请勿泄露。</div>
                </dd>
            </dl>
         </div>
        </form>
         <div class="setBtn"><a href="javascript:;" class="btn-big" onclick="do_binding_mobile()">保存</a></div>
    </div>
</div>
<script type="text/javascript">
var lock_moblie = 0;
var timefun_moblie = 0;
var send_moblie_msg = function () {
  if (lock_moblie == 1) {
    return false;
  }
  var mobile = $('#write_mobile').val().replace(/\s*/,'');
  if (mobile === '') {
    $.ui.showMask("手机号不能为空",true);
    //ui.error('手机号不能为空');
    return false;
  }
  if( !(/^[1][358]\d{9}$/.test(mobile)) ){
    $.ui.showMask("请填写正确的11位手机号",true);
    //ui.error('请填写正确的11位手机号');
    return false;
  }
  $.post(U('public/Register/isPhoneAvailable'), {'phone':mobile}, function(res) {
    if (res.status) {
      if (lock_moblie == 1) {
        return false;
      } else {
        timenum_moblie = 60;
        clearInterval(timefun_moblie);
        lock_moblie = 1;
      }
      $.ajax({
            type : 'post',
            url : U('w3g/Setting/getCaptcha'),
            data : {'type':'mobile', 'mobile':mobile},
            dataType : 'json',
            success : function(res){
                  if (res.status == 0) {
                    lock_mobile = 0;
                    $.ui.showMask(res.info,true);
                    //ui.error(res.info);
                  } else {
                    $('#mobile_msg_tip').show();
                    timefun_mobile = setInterval(timeout_mobile, 1000);
                    //$.ui.showMask("验证码发送成功，请耐心等待",true);
                    //ui.success('邮件发送成功，请耐心等待');
                  }
            }
        });
      /*$.post(U('w3g/Setting/getCaptcha'), {type:'mobile', mobile:mobile}, function(res) {
        if (res.status == 0) {
          lock_moblie = 0;
          $.ui.showMask(res.info,true);
          //ui.error(res.info);
        } else {
          $('#mobile_msg_tip').show();
          timefun_moblie = setInterval(timeout_mobile, 1000);
          $.ui.showMask("短信发送成功，请耐心等待",true);
          //ui.success('短信发送成功，请耐心等待');
        }
      },'json');*/
    } else {
      $.ui.showMask(res.info,true);
      //ui.error(res.info);
    }
  }, 'json');
  return false;
};
var do_binding_mobile = function () {
  var mobile = $('#write_mobile').val().replace(/\s*/,'');
  if (mobile === '') {
    $.ui.showMask('手机号不能为空', true);
    return false;
  }
  if( !(/^[1][358]\d{9}$/.test(mobile)) ){
    $.ui.showMask('请填写正确的11位手机号',true);
    return false;
  }
  var mobile_code = $.trim($('#write_mobile_code').val());
  if (mobile_code === '' || mobile_code.length != 4) {
    $.ui.showMask('验证码错误，请检查验证码', true);
    return false;
  }
  $.ajax({
    type : 'post',
    url : U('w3g/Setting/doBindingMobile'),
    data: $('#do_binding_mobile').serialize(),
    timeout : 3000,
    dataType: 'json',
    success : function(d){
        if(d && d.status){
            $.ui.showMask(d.info,true);
            location.href=U('w3g/Setting/security');
        }
        else{
            $.ui.showMask(d.info,true);
        }
    },
    error: function(r){
        $.ui.showMask("连接服务器失败，请重试:)",true);
    }
  });
};

var timeout_mobile = function () {
  timenum_mobile -= 1;
  if (timenum_mobile < 0) {
    clearInterval(timefun_mobile);
    timefun_mobile = '';
    lock_mobile = 0;
    $('#mobile_msg_tip').hide();
    $('#get_mobile_code').find('span').text('获取验证码');
    return;
  }
  $('#get_mobile_code').find('span').text('('+timenum_mobile+')获取验证码');
};
</script>
<include file="../Public/footer" />